package com.blackhorse.order;

import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import com.blackhorse.order.excpetion.ExceptionUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableEurekaClient//激活Eureka客户端
@SpringBootApplication//订单微服务 启动类
@EntityScan("com.blackhorse.order.entity")//Spring Data Jpa 实体类包扫描
public class OrderRibbonApplication {
    /**
     * sentinel支持对restTemplate的服务调用方法进行熔断保护
     * 只需在RestTemplate的创建方法加注解@SentinelRestTemplate
     * <p>
     * 受保护的资源名：
     * httpmethod:schema://host:port/path ：协议、主机、端口和路径
     * httpmethod:schema://host:port ：协议、主机和端口
     *
     * @SentinelRestTemplate 异常降级
     * fallback：降级方法名
     * fallbackClass: 降级配置类
     * 限流熔断
     * blockHandler：降级方法名
     * blockHandlerClass: 降级配置类
     */
    @SentinelRestTemplate(
            fallback = "handleFallback", fallbackClass = ExceptionUtils.class,
            blockHandler = "handleBlock", blockHandlerClass = ExceptionUtils.class
    )
    @Bean(name = "restTemplate")
    @LoadBalanced //ribbon负载均衡
    public RestTemplate createRestTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(OrderRibbonApplication.class, args);
    }
}

